<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>滚动事件</title>
    <style>
        #header {
            width: 100%;
            height: 100px;
            background-color: #ff0;
            display: none;
        }
    </style>
</head>

<body style="height: 2000px;">
    <header id="header"></header>
    <script>
        //滚动的方法
        function showTop() {
            //debugger;
            //距离顶部的距离
            let srollTop = document.body.scrollTop || document.documentElement.scrollTop;
            console.log(srollTop)
            let head = document.getElementById("header");
            //如果大于>500,头部导航显示，并且固定定位
            if (srollTop > 500) {
                head.style.display = "block";
                head.style.position = "fixed"
            }
        }
        //滚动事件
        window.onscroll = showTop;

        //监听窗口方法
        function resizeWidth() {
            //获取宽度
            let width = window.innerWidth;
            let head = document.getElementById("header");
            //如果小于768,改变窗口的颜色
            if (width < 768) {
                head.style.backgroundColor = "pink"
            }
        }

        //窗口事件
        window.onresize = resizeWidth
    </script>

</body>

</html>